﻿using System.Security.Cryptography.X509Certificates;
using System;

namespace CSharpSM
{
    internal class Program
    {
        static void Main(string[] args)
        {
            sm2Test();
            sm3Test();
            sm4Test();

        }
        static void sm2Test() {
            var keys = Sm2Util.GenerateKeyPair();
            string pubKey = "0436c4037767144fee31f029f82fa6da4cb465e91b92119c56882231b79e1042d3d05a6d59851371fc375fad999114431aa436ada10716f1f20c0309cff6d14cf3"; // keys[0];
            string priKey = "76b5e6481748775cc608fd5c8b69fba13fa2696729eaf8638f8721b9fb222ecd";// keys[1];
            Console.WriteLine("公钥：" + pubKey);
            Console.WriteLine("私钥：" + priKey);
            string srcData = "Hello中国！#￥- 来自C#";
            string encData = Sm2Util.Encrypt(srcData, pubKey);
            Console.WriteLine("密文：" + encData);
            string distData = Sm2Util.Decrypt(encData, priKey);
            Console.WriteLine("解密：" + distData);
            encData = "e13a86ad2c83d6e5debc58f346237ec10d2e2e65f52f50746a8793f3f39207bafc2f3890d836e99371b6e9221fa162c31e05db3b24a6222b772fcbc899be7679469b68d294579dbd208a791f3de080d17ad53ffd1127cb449a6e2ef6ea1eac9e412e64bf90c1d064bfcce82607fc5c736bf4ee4d448043a5267d035c5569";
            distData = Sm2Util.Decrypt(encData, priKey);
            Console.WriteLine("解密三方：" + distData);
            string data = "弱小和无知不是生存的障碍，傲慢才是！！！";
            String signStr = Sm2Util.Sign(data, priKey);
            Console.WriteLine("签名:" + signStr);
            bool verify = Sm2Util.Verify(data, signStr, pubKey);
            Console.WriteLine("验签:" + verify);
            verify = Sm2Util.Verify(data, "3046022100d3bb2c9ba08870fcb8e10ab653d0109d20c75cd6cf099e7fa135c5181fd36048022100abcbc467954db3c1acf43b9019ad818ffddfc72215506cef8f158915406cb200", pubKey);
            Console.WriteLine("验签三方:" + verify);
            Console.WriteLine("===============================");
        }
        static void sm3Test() {
            Console.WriteLine(Sm3Util.Digest("1234567890"));
            Console.WriteLine("===============================");
        }
        static void sm4Test() {
            String key = "496e4956793050595170417037303073"; // Sm4Util.genKeyHex();
            Console.WriteLine("key:" + key);
            String data = "hello world! 水滴 -来自C#";
            String encData = Sm4Util.Encrypt(data, key);
            Console.WriteLine("密文:" + encData);
            String distData = Sm4Util.Decrypt(encData, key);
            Console.WriteLine("解密:" + distData);
            String encDataFront = "1867cb0a32af4f3962c29ac316a39116ccca21c34757da8e2e99ae8165e55b4bca0b2cbc167fc2a0aaa05e362df333fa5a0f403562be8c90e5d8ae8a02a8cfb1b94b7aecc8f36a738a405fb31bfee2aa";
            Console.WriteLine("来自前端密文:" + encDataFront);
            Console.WriteLine(Sm4Util.Decrypt(encDataFront, key)); 
        }
    }

}